﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using A2.ERP.BLL;
using A2.ERP.UI.Filters;
using A2.ERP.Models;
using System.Linq.Expressions;
using System.Text;

namespace A2.ERP.UI.Controllers
{
    public class SettingController : Controller
    {
        #region Services
        private readonly IBLL.Iv_UserService _vUserService = DBService.GetService<v_UserService>();
        private readonly IBLL.It_UserService _userService = DBService.GetService<t_UserService>();
        private readonly IBLL.It_GroupService _groupService = DBService.GetService<t_GroupService>();
        private readonly IBLL.It_logService _logService = DBService.GetService<t_logService>();
        private readonly IBLL.It_UserTypeService _userTypeService = DBService.GetService<t_UserTypeService>();
        private readonly IBLL.It_UserTypeSubClassService _userTypeSubClassService = DBService.GetService<t_UserTypeSubClassService>();
        private readonly IBLL.It_ProductsTypeService _productsTypeService = DBService.GetService<t_ProductsTypeService>();
        private readonly IBLL.It_ProductsBrandService _productsBrandService = DBService.GetService<t_ProductsBrandService>();
        private readonly IBLL.It_ProductsService _productsService = DBService.GetService<t_ProductsService>();
        private readonly IBLL.It_SupplierService _supplierService = DBService.GetService<t_SupplierService>();
        private readonly IBLL.Iv_StoreHouseService _vStoreHouseService = DBService.GetService<v_StoreHouseService>();
        private readonly IBLL.It_StoreHouseService _storeHouseService = DBService.GetService<t_StoreHouseService>();
        private readonly IBLL.It_NoticeService _noticeService = DBService.GetService<t_NoticeService>();
        private readonly IBLL.It_GroupAuthorityService _groupAuthorityService = DBService.GetService<t_GroupAuthorityService>();
        private readonly IBLL.It_AuthorityService _authorityService = DBService.GetService<t_AuthorityService>();

        #endregion
        public ActionResult SysUserManager()
        {
            ViewBag.DataList = _vUserService.Procedures<v_User>(@"select *, tu.TypeName, g.GroupName from t_user u
                join t_Group g on u.GroupID = g.GroupID
                join t_UserType tu on tu.TypeID = u.TypeID
                join t_UserTypeSubClass uts on tu.TypeID = uts.UserTypeID");
            return PartialView();
        }

        public ActionResult CheckUser(string id)
        {
            ViewBag.userInfo = _vUserService.LoadEntities(i => i.UserName == id).FirstOrDefault();

            return PartialView();
        }

        [HttpPost]
        public JsonResult ChangeUserState([System.Web.Http.FromBody]string id, [System.Web.Http.FromBody]string state)
        {
            // 每次请求都会改变状态在原基础上取反
            state = state == "N" ? "Y" : "N";

            var result = _userService.UpdateEntity(new Models.t_User
            {
                UserName = id,
                State = state
            }, i => i.State);

            return Json(result);
        }

        public ActionResult EditUser(string id)
        {
            ViewBag.user = _userService.LoadEntities(i => i.UserName == id).FirstOrDefault();
            ViewBag.group = _groupService.LoadEntities(i => true);
            return PartialView();
        }

        [HttpPut]
        public JsonResult SysUserManager(t_User user)
        {
            var result = _userService.UpdateEntity(user, new Expression<Func<t_User, object>>[]
            {
                i => i.GroupID,
                i => i.UserName,
                i => i.WangWang,
                i => i.State,
                i => i.QQ,
                i => i.RealName,
                i => i.Address,
                i => i.Description
            });

            return Json(result);
        }


        #region GroupManager
        public ActionResult GroupManager()
        {
            ViewBag.groups = _groupService.LoadEntities(i => true).ToList();

            return PartialView();
        }

        [HttpPost]
        public JsonResult GroupManager(t_Group entity)
        {
            var result = _groupService.AddEntity(entity);

            return Json(result);
        }

        [HttpDelete]
        public JsonResult GroupManager(int id)
        {
            try
            {
                var result = _groupService.DeleEntity(new t_Group { GroupID = id });

                return Json(new { result });
            }
            catch (Exception e)
            {

                return Json(new { result = "fasle", error = e.ToString() });
            }
        }

        //[HttpPut]
        public ActionResult EditGroup(int id)
        {
            ViewBag.groupId = id;
            ViewBag.groupAuthority = _groupAuthorityService.LoadEntities(i => i.GroupID == id).ToList();
            ViewBag.authority = _authorityService.LoadEntities(i => true).ToList();

            return View();
        }
        [HttpPut]
        public ActionResult EditGroup(int[] id, int groupId)
        {
            StringBuilder insertSql = new StringBuilder("insert into t_GroupAuthority values");
            StringBuilder delSql = new StringBuilder("delete t_GroupAuthority where groupid = ");
            delSql.Append(groupId);
            //删除所有后添加(刷新数据)
            _groupService.Procedures<int>(delSql.ToString());


            foreach (int i in id)
            {
                insertSql.Append(string.Format("('{0}', '{1}'),", i, groupId));

            }

            insertSql.Remove(insertSql.Length - 1, 1);

            _groupService.Procedures<int>(insertSql.ToString());

            return Json("true");
        }

        #endregion

        #region UserType

        public ActionResult SysUserTypeManager()
        {
            ViewBag.userTypeSubClass = _userTypeSubClassService.LoadEntities(i => true).ToList();
            ViewBag.userType = _userTypeService.LoadEntities(i => true).ToList();

            return PartialView();
        }
        [HttpPost]
        public JsonResult SysUserTypeManager(t_UserTypeSubClass entity)
        {
            var result = _userTypeSubClassService.AddEntity(entity);

            return Json(result);
        }

        [HttpDelete]
        public JsonResult SysUserTypeManager(int id)
        {
            var result = _userTypeSubClassService.DeleEntity(new t_UserTypeSubClass { SubClassID = id });

            return Json(result);
        }

        [HttpPut]
        public JsonResult EditUserType(t_UserTypeSubClass entity)
        {
            var result = _userTypeSubClassService.UpdateEntity(entity);

            return Json(result);
        }
        #endregion

        #region ProductsType
        public ActionResult SysProductsTypeManager()
        {
            ViewBag.productsType = _productsTypeService.LoadEntities(i => true).ToList();
            return PartialView();
        }
        public JsonResult EditProductsType(int id)
        {
            var data = _productsTypeService.LoadEntities(i => i.TypeID == id).FirstOrDefault();
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult SysProductsTypeManager(t_ProductsType entity)
        {
            var result = _productsTypeService.AddEntity(entity);

            return Json(result);
        }

        [HttpDelete]
        public JsonResult SysProductsTypeManager(int id)
        {
            var result = _productsTypeService.DeleEntity(new t_ProductsType { TypeID = id });

            return Json(result);
        }

        [HttpPut]
        public JsonResult EditProductsType(t_ProductsType entity)
        {
            var result = _productsTypeService.UpdateEntity(entity);

            return Json(result);
        }
        #endregion

        #region ProductwsBrand
        public ActionResult SysProductsBrandManager()
        {
            ViewBag.productsBrand = _productsBrandService.LoadEntities(i => true).ToList();
            return PartialView();
        }

        [HttpPost]
        public JsonResult SysProductsBrandManager(t_ProductsBrand entity)
        {
            var result = _productsBrandService.AddEntity(entity);

            return Json(result);
        }

        [HttpDelete]
        public JsonResult SysProductsBrandManager(int id)
        {
            var result = _productsBrandService.DeleEntity(new t_ProductsBrand { BrandID = id });

            return Json(result);
        }

        public JsonResult EditProductsBrand(int id)
        {
            var result = _productsBrandService.LoadEntities(i => i.BrandID == id).FirstOrDefault();

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPut]
        public JsonResult EditProductsBrand(t_ProductsBrand entity)
        {
            var result = _productsBrandService.UpdateEntity(entity);

            return Json(result);
        }
        #endregion

        public ActionResult SystemLog(int pageIndex = 1, int pageSize = 10)
        {
            int totalCount;
            ViewBag.logs = _logService.LoadPageEntities(pageIndex, pageSize, out totalCount, i => true, i => i.LogID, false).ToList();
            ViewBag.totalCount = totalCount;
            ViewBag.currentIndex = pageIndex;
            return PartialView();
        }

        #region Products
        public ActionResult SysProductsManager(string keyword = "")
        {
            ViewBag.products = _productsService.LoadEntities(i => i.ProductsName.Contains(keyword)).ToList();
            return PartialView();
        }

        [HttpPost]
        public JsonResult SysProductsManager(t_Products entity)
        {
            var result = _productsService.AddEntity(entity);

            return Json(result);
        }

        [HttpDelete]
        public JsonResult SysProductsManager(int id)
        {
            var result = _productsService.DeleEntity(new t_Products { ProductsID = id });

            return Json(result);
        }

        public ActionResult EditProducts(int id)
        {
            var data = _productsService.LoadEntities(i => i.ProductsID == id).FirstOrDefault();

            ViewBag.model = data;

            return PartialView();
        }

        [HttpPut]
        public JsonResult EditProducts(t_Products entity)
        {
            var result = _productsService.UpdateEntity(entity);

            return Json(result);
        }
        #endregion

        #region Supplier

        public ActionResult SysSupplierManager()
        {
            ViewBag.supplier = _supplierService.LoadEntities(i => true).ToList();
            return PartialView();
        }

        [HttpPost]
        public JsonResult SysSupplierManager(t_Supplier entity)
        {
            var result = _supplierService.AddEntity(entity);

            return Json(result);
        }

        [HttpDelete]
        public JsonResult SysSupplierManager(int id)
        {
            var result = _supplierService.DeleEntity(new t_Supplier { SupplierID = id });

            return Json(result);
        }

        public ActionResult EditSupplier(int id)
        {
            var model = _supplierService.LoadEntities(i => i.SupplierID == id).FirstOrDefault();

            return PartialView(model);
        }

        [HttpPut]
        public JsonResult EditSupplier(t_Supplier entity)
        {
            var result = _supplierService.UpdateEntity(entity);

            return Json(result);
        }
        #endregion


        public ActionResult DetopManager()
        {
            ViewBag.v_stroeHouse = _vStoreHouseService.LoadEntities(i => true).ToList();
            ViewBag.stroeHouse = _storeHouseService.LoadEntities(i => true).ToList();

            return PartialView();
        }

        [HttpPost]
        public JsonResult DetopManager(t_Supplier entity)
        {
            var result = _supplierService.AddEntity(entity);

            return Json(result);
        }

        #region Notice
        public ActionResult NoticeModule()
        {
            ViewBag.notices = _noticeService.LoadEntities(i => true).ToList();
            return PartialView();
        }

        [HttpPost]
        public JsonResult NoticeModule(t_Notice entity)
        {
            var result = _noticeService.AddEntity(entity);

            return Json(result);
        }

        [HttpDelete]
        public JsonResult NoticeModule(int id)
        {
            var result = _noticeService.DeleEntity(new t_Notice { NoticeID = id });

            return Json(result);
        }
        public ActionResult EditNotice(int id = 0)
        {
            var data = _noticeService.LoadEntities(i => i.NoticeID == id).FirstOrDefault();

            ViewBag.model = data;

            return PartialView();
        }

        [HttpPut]
        public JsonResult EditNotice(t_Notice entity)
        {
            var result = _noticeService.UpdateEntity(entity);

            return Json(result);
        }
        #endregion

    }
}
